<?php
/**
 * 匠言知识付费
 * Author: 山西匠言网络科技有限公司
 * This is not a free software, it under the license terms, you can visit https://www.zsffzxkc.cn/ get more details.
 */
namespace app\admin\controller;
use think\Db;
use think\Loader;

/**
 * Class 用户管理
 * @package app\admin\controller
 */
class Vip extends Base
{
    public function classify_list_json(){
        $map2=array(
            'status'=>1,
            'acid'=>1,
            'pid'=>0
        );
        $classify=Db::name('vip_classify')
            ->where($map2)
            ->select();
//        array_unshift($classify,['id'=>0,'title'=>'总部']);
        foreach ($classify as $k=>$v){
            $list[$k]['value']=$v['id'];
            $list[$k]['label']=$v['title'];
            if($v['id'] != 0){
                $child=Db::name('vip_classify')
                    ->where([
                        'status'=>1,
                        'acid'=>1,
                        'pid'=>$v['id']
                    ])
                    ->select();
                if (!empty($child)){
                    foreach ($child as $key=>$val){
                        $list1[$key]['value']=$val['id'];
                        $list1[$key]['label']=$val['title'];
                    }
                    $list[$k]['children']=$list1;
                }
            }
        }
        $this->result(0,'成功',$list,0);
    }
    /**
     * 导入会员
     */
    public function daoru_user(){
        if(request()->isPost()){
            $post = input('post.');
            $post['formData'] = json_decode($post['formData'],true);
            $file = request()->file('file');
            $info  = $file->getInfo();
            //取出图片后缀
            $type = explode(".",$info['name']);
            $type = end($type);
            $allowTypes =[
                'xls',
                'xlsx',
            ];
            if(!in_array($type,$allowTypes)){
                return '上传格式违法!';
            }
            if ($info) {
                //获取文件所在目录名
                $path = $info['tmp_name'];
                //加载PHPExcel类
                Loader::import('PHPExcel.PHPExcel');
                Loader::import('PHPExcel.PHPExcel.IOFactory.PHPExcel_IOFactory');
                $objPHPExcel =new \PHPExcel();
                if($type == 'xlsx'){
                    $objReader = \PHPExcel_IOFactory::createReader("Excel2007");
                }else{
                    $objReader = \PHPExcel_IOFactory::createReader("Excel5");
                }
                $objPHPExcel = $objReader->load($path, $encode = 'utf-8'); //获取excel文件
//                var_dump($objPHPExcel);die;
                $sheet = $objPHPExcel->getSheet(0); //激活当前的表
                $highestRow = $sheet->getHighestRow(); // 取得总行数
                $highestColumn = $sheet->getHighestColumn(); // 取得总列数
                $a = 0;
                $set=$this->get_set('BASE');
                //将表格里面的数据循环到数组中
                //*为什么$i=2? (因为Excel表格第一行应该是标题，答案，解析等，从第二行开始，才是我们要的数据。)
                for ($i = 2; $i <= $highestRow; $i++) {
                    $data[$a]['realname'] = $objPHPExcel->getActiveSheet()->getCell("A" . $i)->getValue();
                    $data[$a]['nickname'] = $objPHPExcel->getActiveSheet()->getCell("A" . $i)->getValue();
                    $phone = $objPHPExcel->getActiveSheet()->getCell("B" . $i)->getValue();
                    $post['pid'] = $objPHPExcel->getActiveSheet()->getCell("C" . $i)->getValue();
                    $data[$a]['age'] = $objPHPExcel->getActiveSheet()->getCell("D" . $i)->getValue();
                    $data[$a]['gender'] = $objPHPExcel->getActiveSheet()->getCell("E" . $i)->getValue();
                    $data[$a]['wechat'] = $objPHPExcel->getActiveSheet()->getCell("F" . $i)->getValue();
                    $data[$a]['alipay'] = $objPHPExcel->getActiveSheet()->getCell("G" . $i)->getValue();
                    $data[$a]['address'] = $objPHPExcel->getActiveSheet()->getCell("H" . $i)->getValue();
                    $data[$a]['organization'] = $objPHPExcel->getActiveSheet()->getCell("I" . $i)->getValue();
                    if($post['pid'] != 0 && $post['pid'] != '' && isset($post['pid'])){
                        $pidinfo = db('user')->where(['acid'=>1,'id'=>$post['pid']])->find();
                        if(!empty($pidinfo)){
                            $data[$a]['path']=$pidinfo['path'].'-'.$post['pid'];
                            $data[$a]['plv']=$pidinfo['plv']+1;
                            $data[$a]['pid'] = $post['pid'];
                        }else{
                            $post['pid'] = 0;
                            $data[$a]['pid'] = $post['pid'];
                        }
                    }else{
                        $post['pid'] = 0;
                        $data[$a]['pid'] = $post['pid'];
                    }

                    $data[$a]['phone'] = $phone;
                    $data[$a]['password'] = md5(substr($phone,-6));
                    $data[$a]['is_fxs'] = $set['fx_auto']==1?1:0;
                    $data[$a]['createtime'] = time();
                    $data[$a]['acid'] = 1;
                    $data[$a]['is_daoru'] =1;
                    $data[$a]['class_id'] = $post['formData']['class_id'];

                    // 这里的数据根据自己表格里面有多少个字段自行决定
                    $a++;
                }
                if(!empty($data)){
                    //往数据库添加数据
                    $res = Db::name('user')->insertAll($data);
                    if($res){
                        $this->result(200,'导入成功','');
                    }else{
                        $this->result(500,'操作失败','');
                    }
                }
            }
        }
        return $this->fetch();
    }
    // 累计学习时间统计
    public function userstudytime(){
        $post=input('param.');
        $map=array(
            'acid'=>1,
            'uid'=>$post['uid']
        );
        $userinfo=db('user')->where(['acid'=>1,'id'=>$post['uid']])->find();
        if(!empty($userinfo)){
            $userinfo=$userinfo;
        }else{
            $userinfo=[];
        }
        $studyinfo=db('study_time')->where($map)->select();
        $vtime=0;
        $atime=0;
        $tutime=0;
        $alltime=0;
        if(!empty($studyinfo)){
            foreach ($studyinfo as $k=>$v){
                $vtime+=$v['videostudytime'];
                $atime+=$v['audiostudytime'];
                $tutime+=$v['tuwenstudytime'];
            }
        }
        $map2=array(
            'acid'=>1,
            'uid'=>$post['uid'],
            'date'=>date('Ymd',time())
        );
        $todayinfo=db('study_time')->where($map2)->find();
        if(!empty($todayinfo)){
            $tvtime=timetotext($todayinfo['videostudytime']);
            $tatime=timetotext($todayinfo['audiostudytime']);
            $ttutime=timetotext($todayinfo['tuwenstudytime']);
            $talltime=timetotext($todayinfo['videostudytime']+$todayinfo['audiostudytime']+$todayinfo['tuwenstudytime']);
        }else{
            $tvtime=0;
            $tatime=0;
            $ttutime=0;
            $talltime=0;
        }
        $alltime=timetotext($vtime+$atime+$tutime);
        $data=array(
            'vtime'=>timetotext($vtime),
            'atime'=>timetotext($atime),
            'tutime'=>timetotext($tutime),
            'alltime'=>$alltime,
            'tvtime'=>$tvtime,
            'tatime'=>$tatime,
            'ttutime'=>$ttutime,
            'talltime'=>$talltime
        );
        $this->assign('userinfo',$userinfo);
        $this->assign('item',$data);
        return $this->fetch();
    }

    /*
     * 提现申请
     * */
    public function tixian(){
        if(request()->isAjax()) {
            $map['acid']=1;
            $Nowpage = input('get.page') ? input('get.page'):1;
            $limits = input("limit")?input("limit"):10;
            $order=input('order');//排序方式
            $data = Db::name('tixian')
                ->where($map)
                ->page($Nowpage, $limits)
                ->order('sqtime desc')
                ->select();
            foreach ($data as $k=>$v){
                $user = Db::name('user')->where(array('id'=>$v['uid']))->find();
                if($v['txtype']==''){
                    $data[$k]['txtype']='不限';
                }elseif($v['txtype']=='card'){
                    $data[$k]['txtype']='银行卡';
                }elseif($v['txtype']=='alipay'){
                    $data[$k]['txtype']='支付宝';
                }elseif($v['txtype']=='wechat'){
                    $data[$k]['txtype']='微信';
                }
                $data[$k]['nickname']=$user['nickname'];
                $data[$k]['khh']=$user['khh'];
                $data[$k]['hzname']=$user['hzname'];
                $data[$k]['txphone']=$user['txphone'];
                $data[$k]['cardnum']=$user['cardnum'];
                $data[$k]['alipay']=$user['alipay'];
                $data[$k]['wechat']=$user['wechat'];
                $data[$k]['sqtime']=date('Y-m-d H:i:s',$v['sqtime']);
                if($v['is_verify'] == 0){
                    $data[$k]['verify'] = '待审核';
                }elseif($v['is_verify'] == 1){
                    $data[$k]['verify'] = '已审核';
                }elseif($v['is_verify'] == 2){
                    $data[$k]['verify'] = '已拒绝';
                }
            }
            $total=Db::name('tixian')->where($map)->count();
            $this->result(0,'成功',$data,$total);
        }
        return $this->fetch();
    }
    /*
     * 同意提现
     * */
    public function agree_tixian(){
        if(request()->isAjax()) {
            if(session('agid')!='1'){
                $this->result('1','暂无权限');
            }else{
                $id = input('param.id');
                $txinfo = Db::name('tixian')->where(['id'=>$id,'acid'=>1])->find();
                if($txinfo['is_verify']==1){
                    $this->result(1,'已审核请勿重复操作');
                }elseif($txinfo['is_verify']==0){
                    $res=Db::name('tixian')->where(['id'=>$id,'acid'=>1])->update(['is_verify'=>1]);
                    if($res){
                        $msg = array(
                            'first' => array(
                                'value' => "提现已成功！",
                                'color' => '#ff510'
                            ),
                            'keyword1' => array(
                                'value' => $txinfo['txnum'],
                                'color' => '#ff510'
                            ),
                            'keyword2' => array(
                                'value' => date('Y-m-d H:i',time()),
                                'color' => '#ff510'
                            ),
                            'remark' => array(
                                'value' => '感谢您的使用，请您再接再厉!',
                                'color' => '#ff510'
                            ),
                        );
                        $set = $this->get_set('BASE');
                        $userinfo=db('user')->where(['acid'=>1,'id'=>$txinfo['uid']])->find();
                        if($userinfo['gzh_openid']!=''){
                            $res2= $this->sendTpl($userinfo['gzh_openid'],$set['msg_txok'],$msg,'');
                        }
                        $this->result(0,'操作成功');
                    }else{
                        $this->result(1,'操作失败');
                    }
                }else{
                    $this->result(1, '非法操作');
                }
            }
        }
    }
    /*
     * 不同意提现
     * */
    public function noagree_tixian(){
        if(request()->isAjax()) {
            if(session('agid')!='1'){
                $this->result('1','暂无权限');
            }else{
                $id = input('param.id');
                $txinfo = Db::name('tixian')->where(['id'=>$id,'acid'=>1])->find();
                if($txinfo['is_verify']==2){
                    $this->result(1,'已拒绝请勿重复操作');
                }elseif($txinfo['is_verify']==0) {
                    $res = Db::name('tixian')->where(['id' => $id, 'acid' => 1])->update(['is_verify' => 2]);
                    if ($res) {
                        $msg = array(
                            'first' => array(
                                'value' => "提现失败！",
                                'color' => '#ff510'
                            ),
                            'keyword1' => array(
                                'value' => $txinfo['txnum'],
                                'color' => '#ff510'
                            ),
                            'keyword2' => array(
                                'value' => date('Y-m-d H:i', time()),
                                'color' => '#ff510'
                            ),
                            'remark' => array(
                                'value' => '您的提现审核已被拒绝,详询客服!',
                                'color' => '#ff510'
                            ),
                        );
                        $set = $this->get_set('BASE');
                        $userinfo = db('user')->where(['acid' => 1, 'id' => $txinfo['uid']])->find();
                        db('user')->where(['acid' => 1, 'id' => $txinfo['uid']])->update(['yongjin' => $userinfo['yongjin'] + $txinfo['txnum']]);
                        if ($userinfo['gzh_openid'] != '') {
                            $res2 = $this->sendTpl($userinfo['gzh_openid'], $set['msg_txok'], $msg, '');
                        }
                        $this->result(0, '操作成功');
                    } else {
                        $this->result(1, '操作失败');
                    }
                }else{
                    $this->result(1, '非法操作');
                }
            }
        }
    }
    /*
     * 删除提现
     * */
    public function del_tixian(){
        if(request()->isAjax()) {
            if(session('agid')!='1'){
                $this->result('1','暂无权限');
            }else{
                $id = input('param.id');
                $res=Db::name('tixian')->where(['id'=>$id,'acid'=>1])->delete();
                if($res){
                    $this->result(0,'操作成功');
                }else{
                    $this->result(1,'操作失败');
                }
            }
        }
    }
    public function viplevel_list_json(){
        $map=array(
            'acid'=>1,
        );
        $tlist=[['value'=>0,'label'=>'不限']];
        $viplevels=Db::name('viplevel')
            ->where($map)
            ->select();
        if(!empty($viplevels)){
            foreach ($viplevels as $k=>$v){
                array_push($tlist,['value'=>$v['id'],'label'=>$v['name'].'及以上']);
            }
        }
        $this->result(0,'成功',$tlist,0);
    }
    public function index(){
        if(request()->isAjax()) {
            $key=input('keyword');
            $flid = input('students_flid');
            if(isset($key)&&$key!=""){
                $map['nickname|phone|id'] = ['like',"%" . $key . "%"];
            }
            if(isset($flid)&&$flid!=""){
                $map['flid'] = $flid;
            }
            $map['acid']=1;
            $Nowpage = input('get.page') ? input('get.page'):1;
            $limits = input("limit")?input("limit"):10;
            $order=input('order');//排序方式
            $data = Db::name('user')
                ->where($map)
                ->page($Nowpage, $limits)
                ->order('createtime desc')
                ->select();
            foreach ($data as $k=>$v){
                if(session('agid')!='1' && session('username')!='admin'){
                    if($v['phone']){
                        $data[$k]['phone']=substr_replace($v['phone'],'*****',3,5);
                    }
                    if($v['avatar']=='' && $v['nickname']==''){
                        $data[$k]['nickname']=substr_replace($v['nickname'],'******',8,6);
                    }
                }
                if($v['is_vip']==1 && $v['viplevelid']!=''){
                    $levelinfo = db('viplevel')->where(['acid' => 1, 'id' => $v['viplevelid']])->find();
                    $data[$k]['viplevelname'] = $levelinfo['name'];
                }
                //单位
                if($v['flid'] != ''){
                    if($v['flid'] == 0){
                        $data[$k]['danwei'] = '总平台';
                    }else{
                        $flid = explode(',',$v['flid']);
                        $danweiinfo = db('vip_classify')->where(['acid' => 1, 'id' => $flid[0]])->find();
                        $danweiinfo1 = db('vip_classify')->where(['acid' => 1, 'id' => $flid[1]])->find();
                        $data[$k]['danwei'] = $danweiinfo['title'].($danweiinfo1['title']!=''?'-':'').$danweiinfo1['title'];
                    }
                }else{
                    $data[$k]['danwei'] = '';
                }
                //班级
                if($v['class_id'] != ''){
                    $flid = explode(',',$v['class_id']);
                    if($flid[0] == 0){
                        $banjiinfo['name'] = '平台';
                    }else{
                        $banjiinfo = db('peixunban')->where(['acid' => 1, 'id' => $flid[0]])->find();
                    }
                    if(isset($flid[1])){
                        $banjiinfo1 = db('classes')->where(['acid' => 1, 'id' => $flid[1]])->find();
                    }else{
                        $banjiinfo1['name'] = '';
                    }
                    $data[$k]['banji'] = $banjiinfo['name'].($banjiinfo1['name']!=''?'-':'').$banjiinfo1['name'];
                }else{
                    $data[$k]['banji'] = '';
                }
                if($v['pid']!=0){
                    $pinfo=db('user')->where('acid',1)->where('id',$v['pid'])->find();
                    $data[$k]['pinfo']=$pinfo['nickname'].'/'.$v['pid'];
                }else{
                    $data[$k]['pinfo']='总店';
                }
                if($v['apptype']=='APP-PLUS'){
                    $data[$k]['apptype']='APP';
                }elseif($v['apptype']=='MP-WEIXIN'){
                    $data[$k]['apptype']='微信小程序';
                }elseif($v['apptype']=='MP-TOUTIAO'){
                    $data[$k]['apptype']='抖音/头条';
                }
                $data[$k]['createtime']=date('Y-m-d H:i:s',$v['createtime']);
                $data[$k]['lasttime']=$v['lasttime']?date('Y-m-d H:i:s',$v['lasttime']):'';
                if($v['nickname'] == '' && $v['is_doctor'] == 2){
                    $data[$k]['nickname'] = $v['realname'];
                }
                if($v['avatar'] == '' && $v['is_doctor'] == 2){
                    $data[$k]['avatar'] = to_media_url($data[$k]['doctor_img']);
                }else{
                    $data[$k]['avatar']=to_media_url($data[$k]['avatar']);
                }

                $data[$k]['ipaddr'] = get_ip_area($data[$k]['last_login_ip']);//根据ip地址获取地域信息
            }
            $total=Db::name('user')->where($map)->count();
            $this->result(0,'成功',$data,$total);
        }
        return $this->fetch();
    }
    public function dls(){
        if(request()->isAjax()) {
            $key=input('keyword');
            if(isset($key)&&$key!=""){
                $map['nickname|phone|dls_name'] = ['like',"%" . $key . "%"];
            }
            $map['acid']=1;
            $Nowpage = input('get.page') ? input('get.page'):1;
            $limits = input("limit")?input("limit"):10;
            $order=input('order');//排序方式
            $data = Db::name('user')
                ->where($map)
                ->where('is_dls',1)
                ->page($Nowpage, $limits)
                ->order('createtime desc')
                ->select();
            foreach ($data as $k=>$v){
                if(session('agid')!='1' && session('username')!='admin'){
                    if($v['dls_phone']){
                        $data[$k]['dls_phone']=substr_replace($v['dls_phone'],'*****',3,5);
                    }
                    if($v['avatar']=='' && $v['viplevelid']!=''){
                        $data[$k]['nickname']=substr_replace($v['nickname'],'******',8,6);
                    }
                }
                $data[$k]['createtime']=date('Y-m-d H:i:s',$v['createtime']);
                $data[$k]['lasttime']=$v['lasttime']?date('Y-m-d H:i:s',$v['lasttime']):'';
                $data[$k]['avatar']=to_media_url($data[$k]['avatar']);
            }
            $total=Db::name('user')->where($map)->where('is_dls',1)->count();
            $this->result(0,'成功',$data,$total);
        }
        return $this->fetch();
    }
    public function dlssh(){
        if(request()->isAjax()) {
            $key=input('keyword');
            if(isset($key)&&$key!=""){
                $map['nickname|phone|dls_name'] = ['like',"%" . $key . "%"];
            }
            $map['acid']=1;
            $Nowpage = input('get.page') ? input('get.page'):1;
            $limits = input("limit")?input("limit"):10;
            $order=input('order');//排序方式
            $data = Db::name('user')
                ->where($map)
                ->where('dls_sh',1)
                ->page($Nowpage, $limits)
                ->order('createtime desc')
                ->select();
            foreach ($data as $k=>$v){
                if(session('agid')!='1' && session('username')!='admin'){
                    if($v['dls_phone']){
                        $data[$k]['dls_phone']=substr_replace($v['dls_phone'],'*****',3,5);
                    }
                    if($v['avatar']=='' && $v['viplevelid']!=''){
                        $data[$k]['nickname']=substr_replace($v['nickname'],'******',8,6);
                    }
                }
                $data[$k]['createtime']=date('Y-m-d H:i:s',$v['createtime']);
                $data[$k]['lasttime']=$v['lasttime']?date('Y-m-d H:i:s',$v['lasttime']):'';
                $data[$k]['avatar']=to_media_url($data[$k]['avatar']);
            }
            $total=Db::name('user')->where($map)->where('dls_sh',1)->count();
            $this->result(0,'成功',$data,$total);
        }
        return $this->fetch();
    }
    public function sjset(){
//        $id=input('param.id');
//        if(isset($id)){
//            $item=Db::name('user')
//                ->where('acid',1)
//                ->where('id',$id)
//                ->find();
//            if(!empty($item)){
//                $this->assign('item',$item);
//            }
//        }else{
//            $this->assign('item',array());
//        }
        if(request()->isPost()){
            $data = input('post.');
            if(isset($data['id'])){
                $data1=array(
                    'pid'=>$data['pid'],
                );
                $item=Db::name('user')
                    ->where('id',$data['id'])
                    ->find();
                if(!empty($item)){
                    $res=Db::name('user')->where('acid',1)->where('id',$data['id'])->update($data1);
                }
                if($res){
                    if($res==0){
                        $this->result('0','无更新');
                    }else{
                        writelog('管理员'.session('username').'修改了用户'.$data['id'].'的信息'.json_encode($data1),200);
                        $this->result('0','修改成功');
                    }
                }else{
                    $this->result('1','修改失败');
                }
            }
        }
    }
    public function fxssh(){
        if(request()->isAjax()) {
            $key=input('keyword');
            if(isset($key)&&$key!=""){
                $map['nickname|phone|dls_name'] = ['like',"%" . $key . "%"];
            }
            $map['acid']=1;
            $Nowpage = input('get.page') ? input('get.page'):1;
            $limits = input("limit")?input("limit"):10;
            $order=input('order');//排序方式
            $data = Db::name('user')
                ->where($map)
                ->where('is_fxs',2)
                ->page($Nowpage, $limits)
                ->order('createtime desc')
                ->select();
            foreach ($data as $k=>$v){
                if(session('agid')!='1' && session('username')!='admin'){
                    if($v['dls_phone']){
                        $data[$k]['dls_phone']=substr_replace($v['dls_phone'],'*****',3,5);
                    }
                    if($v['avatar']=='' && $v['viplevelid']!=''){
                        $data[$k]['nickname']=substr_replace($v['nickname'],'******',8,6);
                    }
                }
                $data[$k]['createtime']=date('Y-m-d H:i:s',$v['createtime']);
                $data[$k]['lasttime']=$v['lasttime']?date('Y-m-d H:i:s',$v['lasttime']):'';
                $data[$k]['avatar']=to_media_url($data[$k]['avatar']);
            }
            $total=Db::name('user')->where($map)->where('dls_sh',1)->count();
            $this->result(0,'成功',$data,$total);
        }
        return $this->fetch();
    }
    public function fxs(){
        if(request()->isAjax()) {
            $key=input('keyword');
            if(isset($key)&&$key!=""){
                $map['nickname|phone'] = ['like',"%" . $key . "%"];
            }
            $map['acid']=1;
            $Nowpage = input('get.page') ? input('get.page'):1;
            $limits = input("limit")?input("limit"):10;
            $order=input('order');//排序方式
            $data = Db::name('user')
                ->where($map)
                ->where('is_fxs',1)
                ->page($Nowpage, $limits)
                ->order('createtime desc')
                ->select();
            foreach ($data as $k=>$v){
                if(session('agid')!='1' && session('username')!='admin'){
                    if($v['phone']){
                        $data[$k]['phone']=substr_replace($v['phone'],'*****',3,5);
                    }
                    if($v['avatar']=='' && $v['viplevelid']!=''){
                        $data[$k]['nickname']=substr_replace($v['nickname'],'******',8,6);
                    }
                }

                if($v['pid']!=0){
                    $pinfo=db('user')->where('acid',1)->where('id',$v['pid'])->find();
                    $data[$k]['pinfo']=$pinfo['nickname'].'/'.$v['pid'];
                }else{
                    $data[$k]['pinfo']='总店';
                }
                $data[$k]['lasttime']=$v['lasttime']?date('Y-m-d H:i:s',$v['lasttime']):'';
                $data[$k]['avatar']=to_media_url($data[$k]['avatar']);
            }
            $total=Db::name('user')->where($map)->where('is_fxs',1)->count();
            $this->result(0,'成功',$data,$total);
        }
        return $this->fetch();
    }
    public function viplevel(){
        if(request()->isAjax()) {
            $Nowpage = input('get.page') ? input('get.page'):1;
            $limits = input("limit")?input("limit"):10;
            $order=input('order');//排序方式
            $data = Db::name('viplevel')
                ->where('acid',1)
                ->page($Nowpage, $limits)
                ->order($order)
                ->select();
            $total=Db::name('viplevel')->where('acid',1)->count();
            $this->result(0,'成功',$data,$total);
        }
        return $this->fetch();
    }
    public function vipleveladd(){
        $id=input('param.id');
        if(isset($id)){
            $item=Db::name('viplevel')
                ->where('id',$id)
                ->where('acid',1)
                ->find();
            if(!empty($item)){
                $this->assign('item',$item);
            }
        }else{
            $this->assign('item',array());
        }
        if(request()->isPost()){
            $data = input('post.');
            if($data['id']!=''){
                $res=Db::name('viplevel')->where('acid',1)->where('id',$data['id'])->update($data);
                if($res){
                    if($res==0){
                        $this->result('0','无更新');
                    }else{
                        $this->result('0','修改成功');
                    }
                }else{
                    $this->result('1','修改失败');
                }
            }else{
                $data['acid']=1;
                $res=Db::name('viplevel')->insert($data);
                if($res){
                    $this->result('0','添加成功');
                }else{
                    $this->result('1','添加失败');
                }
            }
        }

        return $this->fetch();
    }
    public function viplevel_del(){
        if(request()->isAjax()) {
            $id = input('param.id');
            $res=Db::name('viplevel')->where('acid',1)->where('id',$id)->delete();
            if($res){
                $this->result(0,'删除成功');
            }else{
                $this->result(1,'删除失败');
            }
        }
    }
    public function timepart_del(){
        if(request()->isAjax()) {
            $id = input('param.id');
            $res=Db::name('viptime')->where('acid',1)->where('id',$id)->delete();
            if($res){
                $this->result(0,'删除成功');
            }else{
                $this->result(1,'删除失败');
            }
        }
    }
    public function timepart(){
        if(request()->isAjax()) {
            $Nowpage = input('get.page') ? input('get.page'):1;
            $limits = input("limit")?input("limit"):10;
            $order=input('order');//排序方式
            $data = Db::name('viptime')
                ->where('acid',1)
                ->page($Nowpage, $limits)
                ->order($order)
                ->select();
            $total=Db::name('viptime')->where('acid',1)->count();
            $this->result(0,'成功',$data,$total);
        }
        return $this->fetch();
    }
    public function timepartadd(){
        $id=input('param.id');
        if(isset($id)){
            $item=Db::name('viptime')
                ->where('id',$id)
                ->where('acid',1)
                ->find();
            if(!empty($item)){
                $this->assign('item',$item);
            }
        }else{
            $this->assign('item',array());
        }
        if(request()->isPost()){
            $data = input('post.');
            if($data['id']!=''){
                $res=Db::name('viptime')->where('acid',1)->where('id',$data['id'])->update($data);
                if($res){
                    if($res==0){
                        $this->result('0','无更新');
                    }else{
                        $this->result('0','修改成功');
                    }
                }else{
                    $this->result('1','修改失败');
                }
            }else{
                $data['acid']=1;
                $res=Db::name('viptime')->insert($data);
                if($res){
                    $this->result('0','添加成功');
                }else{
                    $this->result('1','添加失败');
                }
            }
        }
        return $this->fetch();
    }
    public function setprice(){
        $id=input('param.id');
        $viplevels = db('viplevel')->where('acid',1)->select();
        foreach ($viplevels as $k=>$v){
            $viplevels[$k]['price'] = 0;
        }
        if(isset($id)){
            $item=Db::name('viptime')
                ->where('id',$id)
                ->where('acid',1)
                ->find();
            if(!empty($item)){
                if($item['setprice'] && $item['setprice']!=''){
                    foreach ($viplevels as $k=>$v){
                        $prices = json_decode($item['setprice'],true);
                        foreach ($prices as $key1=>$val1){
                            $lid=substr($key1,5);
                            if($v['id'] == $lid){
                                $viplevels[$k]['price'] = $val1;
                            }
                        }
                    }
                }else{
                    foreach ($viplevels as $k=>$v){
                        $viplevels[$k]['price'] = 0;
                    }
                }
                $this->assign('item',$item);
            }
        }else{
            $this->assign('item',array());
        }

        $this->assign('viplevels',$viplevels);
        if(request()->isPost()){
            $data = input('post.');
            if($data['id']!=''){
                $id=$data['id'];
                unset($data['id']);
                $data = json_encode($data);
                $res=Db::name('viptime')->where('acid',1)->where('id',$id)->setField('setprice',$data);
                if($res){
                    if($res==0){
                        $this->result('0','无更新');
                    }else{
                        $this->result('0','修改成功');
                    }
                }else{
                    $this->result('1','修改失败');
                }
            }
        }
        return $this->fetch();
    }
    public function viptime_list_json(){
        $teachers=Db::name('viptime')
            ->where('acid',1)
            ->select();
        foreach ($teachers as $k=>$v){
            $tlist[$k]['value']=$v['id'];
            $tlist[$k]['label']=$v['name'];
        }
        $this->result(0,'成功',$tlist,0);
    }
    public function vipclass_list_json(){
        $teachers=Db::name('viplevel')
            ->where('acid',1)
            ->select();
        foreach ($teachers as $k=>$v){
            $tlist[$k]['value']=$v['id'];
            $tlist[$k]['label']=$v['name'];
        }
        $this->result(0,'成功',$tlist,0);
    }
    public function vipedit(){
        $id=input('param.id');
        if(isset($id)){
            $item=Db::name('user')
                ->where('acid',1)
                ->where('id',$id)
                ->find();
            if(!empty($item)){
                $this->assign('item',$item);
            }
        }else{
            $this->assign('item',array());
        }
        if(request()->isPost()){
            $data = input('post.');
            if(session('uid')!=1){
                $this->result('1','暂无权限');
            }
            if(isset($data['id'])){
                $data1=array(
                    'money'=>($data['adjust_money']!=''?$data['adjust_money']:0)+($data['money']??0),
                    'credit'=>($data['adjust_credit']!=''?$data['adjust_credit']:0)+($data['credit']??0),
                    'phone'=>$data['phone'],
                    'flid'=>$data['flid'],
                );
                $item=Db::name('user')
                    ->where('id',$data['id'])
                    ->find();
                if(!empty($item)){
                    $res=Db::name('user')->where('acid',1)->where('id',$data['id'])->update($data1);
                }
                if($res){
                    if($res==0){
                        $this->result('0','无更新');
                    }else{
                        writelog('管理员'.session('username').'修改了用户'.$data['id'].'-'.$item['nickname'].'的信息'.json_encode($data1),200);
                        $this->result('0','修改成功');
                    }
                }else{
                    $this->result('1','修改失败');
                }
            }
        }
        return $this->fetch();
    }
    public function vipset(){
        $id=input('param.id');
        if(isset($id)){
            $item=Db::name('user')
                ->where('acid',1)
                ->where('id',$id)
                ->find();
            if(!empty($item)){
                $this->assign('item',$item);
            }
        }else{
            $this->assign('item',array());
        }
        if(request()->isPost()){
            if(session('uid')!=1){
                $this->result('1','暂无权限');
            }
            $data = input('post.');
            if(isset($data['id'])){
                $data1=array(
                    'is_vip'=>1,
                    'vip_state'=>'正常',
                    'viplevelid'=>$data['viplevelid'],
                    'viptypeid'=>$data['viptypeid'],
                    'vipsj_fangshi'=>'admin',
                    'vipsj_time'=>time()
                );

                $item=Db::name('user')
                    ->where('id',$data['id'])
                    ->find();
                if(!empty($item)){
                    $res=Db::name('user')->where('acid',1)->where('id',$data['id'])->update($data1);
                }
                if($res){
                    if($res==0){
                        $this->result('0','无更新');
                    }else{
                        writelog('管理员'.session('username').'设置了用户'.$data['id'].'的VIP信息'.json_encode($data1),200);
                        $this->result('0','修改成功');
                    }
                }else{
                    $this->result('1','修改失败');
                }
            }
        }
        return $this->fetch();
    }
//    设置vip
    public function add_vip(){
        $id=input('param.id');
        if(isset($id)){
            $item=Db::name('user')
                ->where('id',$id)
                ->find();
            if(!empty($item)){
                $this->assign('item',$item);
            }
        }else{
            $this->assign('item',array());
        }
        if(request()->isPost()){
            $data = input('post.');
            unset($data['file']);
            if(session('uid')!=1){
                $this->result('1','暂无权限');
            }else{
                if($data['id']!=''){
                    $item=Db::name('user')
                        ->where('id',$data['id'])
                        ->find();
                    if(!empty($item)){
                        $res=Db::name('user')->where('id',$data['id'])->update($data);
                    }
                    if($res){
                        if($res==0){
                            $this->result('0','无更新');
                        }else{
                            writelog('管理员'.session('username').'设置了用户'.$data['id'].'的VIP信息'.json_encode($data),200);
                            $this->result('0','修改成功');
                        }
                    }else{
                        $this->result('1','修改失败');
                    }
                }
            }
        }
        return $this->fetch();
    }
//    取消vip
    public function cancel_vip(){
        if(request()->isAjax()) {
            if(session('uid')!='1'){
                $this->result('1','暂无权限');
            }else{
                $id = input('param.id');
                $user=db('user')->where('id',$id)->find();
                if(!empty($user)){
                    $data = [
                        'is_vip'=>0,
                        'vip_state'=>'',
                        'viplevelid'=>'',
                        'viptypeid'=>'',
                        'vipsj_fangshi'=>'',
                        'vipsj_orderid'=>'',
                        'vipsj_time'=>'',
                    ];
                    $res=Db::name('user')->where('id',$id)->update($data);
                    if($res){
                        $this->result(0,'取消vip成功');
                        writelog('管理员'.session('username').'取消了用户'.$data['id'].'的vip'.json_encode($data),200);
                    }else{
                        $this->result(1,'取消失败');
                    }
                }
            }
        }
    }
    public function vip_del(){
        if(request()->isAjax()) {
            if(session('agid')!='1'){
                $this->result('1','暂无权限');
            }else{
                $id = input('param.id');
                $user=db('user')->where('id',$id)->find();
                if(!empty($user)){
//                    $res=Db::name('user')->where('id',$id)->delete();
                    $res=Db::name('user')->where('id',$id)->update(['status'=>2]);
                    if($res){
                        $this->result(0,'删除成功');
                        writelog('管理员'.session('username').'删除了用户'.$id.'-'.$user['nickname'],200);
                    }else{
                        $this->result(1,'删除失败');
                    }
                }
            }
        }
    }
    //重置修改密码为123456
    public function edit_password(){
        if(request()->isAjax()) {
            if(session('uid')!='1'){
                $this->result('1','暂无权限');
            }else{
                $id = input('param.id');
                $user=db('user')->where('id',$id)->find();
                if(!empty($user)){
                    $res=Db::name('user')->where('id',$id)->update(['password'=>md5('123456'.substr($user['phone'],-6))]);
                    writelog('管理员'.session('username').'重置了用户'.$id.'-'.$user['nickname'].'的密码',200);
                }
                if($res){
                    $this->result(0,'操作成功');
                }else{
                    $this->result(1,'操作失败');
                }
            }
        }
    }
    public function vip_pullblack(){
        if(request()->isAjax()) {
            if(session('uid')!='1'){
                $this->result('1','暂无权限');
            }else{
                $id = input('param.id');
                $user=db('user')->where('id',$id)->find();
                if(!empty($user)){
                    if($user['status']==1){
                        $res=Db::name('user')->where('id',$id)->update(['status'=>0]);
                        writelog('管理员'.session('username').'恢复了用户'.$id.'-'.$user['nickname'],200);
                    }else{
                        $res=Db::name('user')->where('id',$id)->update(['status'=>1]);
                        writelog('管理员'.session('username').'拉黑了用户'.$id.'-'.$user['nickname'],200);
                    }
                }
                if($res){
                    $this->result(0,'操作成功');
                }else{
                    $this->result(1,'操作失败');
                }
            }
        }
    }
    public function dlssh_ajax(){
        $id = input('param.id');
        if(session('uid')!='1'){
            $this->result('1','暂无权限');
        }
        if(request()->isGet()){
            $data=array(
                'is_dls'=>1,
                'dls_sh'=>2
            );
            $item=Db::name('user')
                ->where('id',$id)
                ->find();
            if(!empty($item)){
                $res=Db::name('user')->where('acid',1)->where('id',$id)->update($data);
            }
            if($res){
                if($res==0){
                    $this->result('0','无更新');
                }else{
                    writelog('管理员'.session('username').'修改了用户'.$data['id'].'的信息'.json_encode($data),200);
                    $this->result('0','修改成功');
                }
            }else{
                $this->result('1','修改失败');
            }
        }
    }
    public function fxssh_ajax(){
        $id = input('param.id');
        if(session('uid')!='1'){
            $this->result('1','暂无权限');
        }
        if(request()->isGet()){
            $data=array(
                'is_fxs'=>1
            );
            $item=Db::name('user')
                ->where('id',$id)
                ->find();
            if(!empty($item)){
                $res=Db::name('user')->where('acid',1)->where('id',$id)->update($data);
            }
            if($res){
                if($res==0){
                    $this->result('0','无更新');
                }else{
                    writelog('管理员'.session('username').'修改了用户'.$data['id'].'的信息'.json_encode($data),200);
                    $this->result('0','修改成功');
                }
            }else{
                $this->result('1','修改失败');
            }
        }
    }
}
